package com.csk.zsk.compents.action;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.csk.zsk.commons.action.AjaxActionSupport;
import com.csk.zsk.commons.utils.web.struts2.Struts2Utils;
import com.csk.zsk.compents.entity.BookResource;
import com.csk.zsk.compents.entity.ComicResource;
import com.csk.zsk.compents.entity.MusicResource;
import com.csk.zsk.compents.entity.NewsResource;
import com.csk.zsk.compents.entity.PicnewsResource;
import com.csk.zsk.compents.entity.User;
import com.csk.zsk.compents.entity.VideoResource;
import com.csk.zsk.compents.service.ResourceService;
import com.csk.zsk.compents.utils.Constants;

@Controller
@Scope("prototype")
public class ResourceAction extends AjaxActionSupport {

	private static final long serialVersionUID = -1590896697025354763L;

	private String resourceType;
	private String author;
	private String chapter;
	private String content;
	private String descp;
	private String title;
	private String urls;
	private String url;
	private String type;

	private String userId;
	private String userName;
	private Integer num;
	private Date startDate;
	private Date endDate;

	@Autowired
	private ResourceService resourceService;

	public void saveResource() {
		User user = (User) Struts2Utils.getSessionAttribute(Constants.USER_SESSION);
		String userId = user.getId();
		String userName = user.getLoginName();
		Date time = new Date();
		try {
			if (resourceType.equals("1")) {
				BookResource bookResource = new BookResource();
				bookResource.setAuthor(author);
				bookResource.setChapter(chapter);
				bookResource.setContent(content);
				bookResource.setDescp(descp);
				bookResource.setTitle(title);
				bookResource.setUserId(userId);
				bookResource.setUserName(userName);
				bookResource.setTime(time);
				resourceService.saveBookResource(bookResource);
			} else if (resourceType.equals("2")) {
				ComicResource comicResource = new ComicResource();
				comicResource.setAuthor(author);
				comicResource.setChapter(chapter);
				comicResource.setUrls(urls);
				comicResource.setDescp(descp);
				comicResource.setTitle(title);
				comicResource.setUserId(userId);
				comicResource.setUserName(userName);
				comicResource.setTime(time);
				resourceService.saveComicResource(comicResource);
			} else if (resourceType.equals("3")) {
				MusicResource musicResource = new MusicResource();
				musicResource.setDescp(descp);
				musicResource.setTitle(title);
				musicResource.setUrls(urls);
				musicResource.setUserId(userId);
				musicResource.setUserName(userName);
				musicResource.setTime(time);
				resourceService.saveMusicResource(musicResource);
			} else if (resourceType.equals("4")) {
				NewsResource newsResource = new NewsResource();
				newsResource.setContent(content);
				newsResource.setDescp(descp);
				newsResource.setTitle(title);
				newsResource.setUserId(userId);
				newsResource.setUserName(userName);
				newsResource.setTime(time);
				resourceService.saveNewsResource(newsResource);
			} else if (resourceType.equals("5")) {
				PicnewsResource picnewsResource = new PicnewsResource();
				picnewsResource.setContent(content);
				picnewsResource.setDescp(descp);
				picnewsResource.setTitle(title);
				picnewsResource.setUserId(userId);
				picnewsResource.setUserName(userName);
				picnewsResource.setTime(time);
				resourceService.savePicnewsResource(picnewsResource);
			} else if (resourceType.equals("6")) {
				VideoResource videoResource = new VideoResource();
				videoResource.setDescp(descp);
				videoResource.setTitle(title);
				videoResource.setType(type);
				videoResource.setUrl(url);
				videoResource.setUserId(userId);
				videoResource.setUserName(userName);
				videoResource.setTime(time);
				resourceService.saveVideoResource(videoResource);
			}
			Struts2Utils.renderText("success");
		} catch (Exception e) {
			Struts2Utils.renderText("fail");
		}
	}

	public void getResources() {
		if (userId == null || "".equals(userId)) {
			User user = (User) Struts2Utils.getSessionAttribute(Constants.USER_SESSION);
			userId = user.getId();
		}
		if (resourceType.equals("1")) {
			List<BookResource> bookResourceList = resourceService.getBookResources(userId, num);
			Struts2Utils.renderJson(bookResourceList);

		} else if (resourceType.equals("2")) {
			List<ComicResource> comicResourceList = resourceService.getComicResources(userId, num);
			Struts2Utils.renderJson(comicResourceList);
		} else if (resourceType.equals("3")) {
			List<MusicResource> musicResourceList = resourceService.getMusicResources(userId, num);
			Struts2Utils.renderJson(musicResourceList);
		} else if (resourceType.equals("4")) {
			List<NewsResource> newsResourceList = resourceService.getNewsResources(userId, num);
			Struts2Utils.renderJson(newsResourceList);
		} else if (resourceType.equals("5")) {
			List<PicnewsResource> picnewsResourceList = resourceService.getPicnewsResources(userId, num);
			Struts2Utils.renderJson(picnewsResourceList);
		} else if (resourceType.equals("6")) {
			List<VideoResource> videoResourceList = resourceService.getVideoResources(userId, num);
			Struts2Utils.renderJson(videoResourceList);
		}
	}

	public void searchResource() {
		if (resourceType.equals("1")) {
			List<BookResource> bookResourceList = resourceService.searchBookResource(title, userName, startDate,
					endDate);
			Struts2Utils.renderJson(bookResourceList);

		} else if (resourceType.equals("2")) {
			List<ComicResource> comicResourceList = resourceService.searchComicResource(title, userName, startDate,
					endDate);
			Struts2Utils.renderJson(comicResourceList);
		} else if (resourceType.equals("3")) {
			List<MusicResource> musicResourceList = resourceService.searchMusicResource(title, userName, startDate,
					endDate);
			Struts2Utils.renderJson(musicResourceList);
		} else if (resourceType.equals("4")) {
			List<NewsResource> newsResourceList = resourceService.searchNewsResource(title, userName, startDate,
					endDate);
			Struts2Utils.renderJson(newsResourceList);
		} else if (resourceType.equals("5")) {
			List<PicnewsResource> picnewsResourceList = resourceService.searchPicnewsResource(title, userName,
					startDate, endDate);
			Struts2Utils.renderJson(picnewsResourceList);
		} else if (resourceType.equals("6")) {
			List<VideoResource> videoResourceList = resourceService.searchVideoResource(title, userName, startDate,
					endDate);
			Struts2Utils.renderJson(videoResourceList);
		}
	}

	public void setResourceType(String resourceType) {
		this.resourceType = resourceType;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public void setChapter(String chapter) {
		this.chapter = chapter;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setDescp(String descp) {
		this.descp = descp;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setUrls(String urls) {
		this.urls = urls;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public void setNum(Integer num) {
		this.num = num;
	}

}
